//
//  SplashViewController.m
//  mUBB
//
//  Created by Cosmin Stirbu on 2/24/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "SplashViewController.h"
#import "StartupManager.h"
#import "WSError.h"
#import "WelcomeViewController.h"
#import "HomeViewController.h"

@interface SplashViewController ()

@end

@implementation SplashViewController

@synthesize backgroundImageView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    // Set background image
    backgroundImageView.image = [UIImage imageNamed:[[ConfigurationManager instance] UIConfigValueForKey:@"splash_image"]];
    
    // Get Entry Points
    [[StartupManager instance] getEntryPointsWithDelegate:self];
}

#pragma mark - Entry Points Delegate Methods

- (void)didReceiveEntryPointsWithSuccess:(id)object forTicket:(Ticket *)ticket {
    // Check if we should display the welcome screen
    if ([[StartupManager instance] shouldPresentWelcomeScreen]) {
        WelcomeViewController *welcomeViewController = [[WelcomeViewController alloc] initWithNibName:@"WelcomeViewController" bundle:nil];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:welcomeViewController];
        navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:navigationController animated:YES];
    } else {
        // Display the Home Screen
        HomeViewController *homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:homeViewController];
        navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        [self presentModalViewController:navigationController animated:YES];
    }
}

- (void)didReceiveEntryPointsWithError:(id)object forTicket:(Ticket *)ticket {
    WSError *error = (WSError *)object;
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:error.errorMessage delegate:self cancelButtonTitle:NSLocalizedString(@"ALERT_CANCEL", nil) otherButtonTitles:NSLocalizedString(@"ALERT_RETRY", nil), nil];
    [alert show];
}

#pragma mark - Alert View Delegate method

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != alertView.cancelButtonIndex) {
        // Get Entry Points
        [[StartupManager instance] getEntryPointsWithDelegate:self];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setBackgroundImageView:nil];
    [super viewDidUnload];
}
@end
